<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/07/16 13:53:50 $)
// $RCSfile: functions_calendar.php,v $ : $Revision: 1.6 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Insert a new Calendar Event
*/
function insertEvent($subject, $eventtype, $ownerstaffid, $isalldayevent, $startdateline, $enddateline, $activitytype, $location, $notes, $calendarstatusid, $calendarlabelid, $calendarcategoryid)
{
	global $dbCore, $_SWIFT;

	if ($isalldayevent)
	{
		$enddateline = mktime(23,59,0,date("m", $startdateline), date("d", $startdateline), date("Y", $startdateline));
	}

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendarevents` (`eventtype`, `ownerstaffid`, `staffid`, `isalldayevent`, `dateline`, `subject`, `startdateline`, `duration`, `enddateline`, `activitytype`, `location`, `calendarstatusid`, `calendarlabelid`, `calendarcategoryid`, `lastupdate`) VALUES ('". $dbCore->escape($eventtype) ."', '". intval($ownerstaffid) ."', '". intval($_SWIFT["staff"]["staffid"]) ."', '". intval($isalldayevent) ."', '". DATENOW ."', '". $dbCore->escape($subject) ."', '". intval($startdateline) ."', '". intval($enddateline-$startdateline) ."', '". intval($enddateline) ."', '". intval($activitytype) ."', '". $dbCore->escape($location) ."', '". intval($calendarstatusid) ."', '". intval($calendarlabelid) ."', '". intval($calendarcategoryid) ."', '". DATENOW ."');");
	$calendareventid = $dbCore->lastInsertId();
	if (!$calendareventid)
	{
		return false;
	}

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendardata` (`datatype`, `typeid`, `contents`) VALUES ('". TEAMWORK_EVENT ."', '". intval($calendareventid) ."', '". $dbCore->escape($notes) ."');");

	return $calendareventid;
}

/**
* Update Calendar Event
*/
function updateEvent($calendareventid, $subject, $eventtype, $ownerstaffid, $isalldayevent, $startdateline, $enddateline, $activitytype, $location, $notes, $calendarstatusid, $calendarlabelid, $calendarcategoryid)
{
	global $dbCore, $_SWIFT;

	if ($isalldayevent)
	{
		$enddateline = mktime(24,0,0,date("m", $startdateline), date("d", $startdateline), date("Y", $startdateline));
	}

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendarevents` SET `eventtype` = '". $dbCore->escape($eventtype) ."', `ownerstaffid` = '". intval($ownerstaffid) ."', `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."', `isalldayevent` = '". intval($isalldayevent) ."', `subject` = '". $dbCore->escape($subject) ."', `startdateline` = '". intval($startdateline) ."', `duration` = '". intval($enddateline-$startdateline) ."', `enddateline` = '". intval($enddateline) ."', `activitytype` = '". intval($activitytype) ."', `location` = '". $dbCore->escape($location) ."', `calendarstatusid` = '". intval($calendarstatusid) ."', `calendarlabelid` = '". intval($calendarlabelid) ."', `calendarcategoryid` = '". intval($calendarcategoryid) ."', `lastupdate` = '". DATENOW ."' WHERE `calendareventid` = '". intval($calendareventid) ."';");

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendardata` SET `contents` = '". $dbCore->escape($notes) ."' WHERE `datatype` = '". TEAMWORK_EVENT ."' AND `typeid` = '". intval($calendareventid) ."';");

	return true;
}

/**
* Delete the given calendar events
*/
function deleteCalendarEvents($calendareventidlist, $guidid = 0)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($calendareventidlist))
	{
		return false;
	}

	$_eventidlist = array();
	$dbCore->query("SELECT `calendareventid` FROM `". TABLE_PREFIX ."calendarevents` WHERE ((`eventtype` = '". SWIFTPUBLIC ."') OR (`eventtype` = '". SWIFTPRIVATE ."' AND `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."')) AND `calendareventid` IN (". buildIN($calendareventidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_eventidlist[] = $dbCore->Record["calendareventid"];
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendarevents` WHERE `calendareventid` IN (". buildIN($_eventidlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendardata` WHERE `datatype` = '". TEAMWORK_EVENT ."' AND `typeid` IN (". buildIN($_eventidlist) .");");

	// First load the links
	$_insertsql = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendarlinks` WHERE `linktype` = '". LINK_EVENT ."' AND `typeid` IN (". buildIN($_eventidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_insertsql[] = "('". TEAMWORK_EVENT ."', '". $dbCore->escape($dbCore->Record["entryid"]) ."', '". DATENOW ."', '". intval($guidid) ."')";
	}

	if (count($_insertsql))
	{
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."twdeletelog` (`entrytype`, `entryid`, `dateline`, `guidid`) VALUES ". implode(",", $_insertsql).";");
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendarlinks` WHERE `linktype` = '". LINK_EVENT ."' AND `typeid` IN (". buildIN($_eventidlist) .");");

	return true;
}
?>